package com.example.minemodule.ui;

import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

import androidx.databinding.DataBindingUtil;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.examination.mlib.constants.ARouterConstants;
import com.examination.mlib.constants.ClickApi;
import com.example.minemodule.R;
import com.example.minemodule.databinding.LogoutPopWindowBinding;
import com.example.minemodule.databinding.SettingActivityBinding;
import com.example.minemodule.entity.ProtocalEntity;
import com.example.minemodule.viewmodel.SettingViewModel;
import com.yilijk.base.base.BaseActivity;
import com.yilijk.base.utils.ClickUtils;
import com.yilijk.base.utils.CommonUtils;
import com.yilijk.base.utils.DevicesUtils;
import com.yilijk.base.utils.LoadingUtils;

import static com.example.minemodule.MineConstants.LOGOUT_RESULT;

/**
 * Created by SHICHENG
 * on 2021/10/22.
 */
@Route(path = ARouterConstants.SettingActivity)
public class SettingActivity extends BaseActivity<SettingViewModel, SettingActivityBinding> {

    private LogoutPopWindowBinding logoutPopWindowBinding;
    private PopupWindow popupWindow;

    @Override
    protected int getContentViewId() {
        return R.layout.activity_setting;
    }

    @Override
    protected void processLogic() {
        setUseStatusBarColor(R.color.Color_white, false, true);
        binding.titleBar.txt_title.setText("设置");
        //设置当前版本号
        binding.tvVersion.setText(DevicesUtils.getVersionName());

        View inflate = getLayoutInflater().inflate(R.layout.logout_pop, null);
        logoutPopWindowBinding = DataBindingUtil.bind(inflate);
        popupWindow = new PopupWindow();
        popupWindow.setWidth(LinearLayout.LayoutParams.WRAP_CONTENT);
        popupWindow.setHeight(LinearLayout.LayoutParams.WRAP_CONTENT);
        popupWindow.setFocusable(true);// 点击空白处时，隐藏掉pop窗口
        popupWindow.setContentView(inflate);
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                CommonUtils.setBackgroundAlpha(SettingActivity.this, 1.0f);
            }
        });

    }

    @Override
    protected void dispostData() {

    }

    @Override
    protected void setListener() {
        ClickUtils.setFastOnClickListener(binding.llAgreement, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ClickUtils.sendEvent(ClickApi.Mine.click_protocal, SettingActivity.this, "用户协议点击");
               getProtocalData("yhzcjfwxy");
            }
        });

        ClickUtils.setFastOnClickListener(binding.llPrivacy, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getProtocalData("flsmjyszccs");
            }
        });
        ClickUtils.setFastOnClickListener(binding.tvLogout, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ClickUtils.sendEvent(ClickApi.Mine.click_logout_dialog, SettingActivity.this, "退出登录点击出现弹窗");
                popupWindow.showAtLocation(v, Gravity.CENTER, 0, 0);
                CommonUtils.setBackgroundAlpha(SettingActivity.this, 0.3f);
            }
        });

        ClickUtils.setFastOnClickListener(logoutPopWindowBinding.cancelLogout, new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                ClickUtils.sendEvent(ClickApi.Mine.click_logout_cancel, SettingActivity.this, "退出登录弹窗取消");
                popupWindow.dismiss();
            }
        });

        ClickUtils.setFastOnClickListener(logoutPopWindowBinding.logout, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ClickUtils.sendEvent(ClickApi.Mine.click_logout_sure, SettingActivity.this, "退出登录弹窗点击确定");
                LoadingUtils.getLoadingUtils(SettingActivity.this).showLoading();
                userLogout();
            }
        });
    }

    /**
     * 请求用户注册及服务协议
     */
    void getProtocalData(String documentNo){
        mViewModel.getProtocalEntity(documentNo).observe(SettingActivity.this,resource -> resource.handler(new OnCallback<ProtocalEntity.DataBean>() {
            @Override
            public void onSuccess(ProtocalEntity.DataBean dataBean) {
                mViewModel.openProtocalWebPage(dataBean);
            }
        }));
    }

    /**
     * 用户退出操作
     */
    void userLogout() {
        mViewModel.getLogoutInfo().observe(SettingActivity.this, resource -> resource.handler(new OnCallback<String>() {
            @Override
            public void onSuccess(String data) {
                LoadingUtils.getLoadingUtils(SettingActivity.this).dismissLoading();
                popupWindow.dismiss();
                setResult(LOGOUT_RESULT);
                finish();
            }
        }));
    }


    @Override
    public void onClick(View v) {

    }
}
